using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The SchoolAddressTableDB class is responsible for interacting with the database to retrieve and store information
/// about SchoolAddressTable objects.
/// </summary>
public class SchoolAddressTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of SchoolAddressTable from the underlying datasource.
/// </summary>
/// <param name="schoolAddressKey">The unique SchoolAddressKey of the SchoolAddressTable in the database.</param>
/// <returns>An SchoolAddressTable when the SchoolAddressKey was found in the database, or null otherwise.</returns>
public static SchoolAddressTable GetItem(int schoolAddressKey)
{
SchoolAddressTable mySchoolAddressTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocSchoolAddressTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@schoolAddressKey", schoolAddressKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
mySchoolAddressTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return mySchoolAddressTable;
}
}

/// <summary>
/// Returns a list with SchoolAddressTable objects.
/// </summary>
/// <returns>A generics List with the SchoolAddressTable objects.</returns>
public static SchoolAddressTableList GetList()
{
SchoolAddressTableList tempList = new SchoolAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocSchoolAddressTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with SchoolAddressTable objects.
/// </summary>
/// <returns>A generics List with the SchoolAddressTable objects.</returns>
public static SchoolAddressTableList GetListBySchoolKey(int SchoolKey)
{
SchoolAddressTableList tempList = new SchoolAddressTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocSchoolAddressTableSelectListBySchoolKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a SchoolAddressTable in the database.
/// </summary>
/// <param name="mySchoolAddressTable">The SchoolAddressTable instance to save.</param>
/// <returns>The new SchoolAddressKey if the SchoolAddressTable is new in the database or the existing SchoolAddressKey when an item was updated.</returns>
public static int Save(SchoolAddressTable mySchoolAddressTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocSchoolAddressTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (mySchoolAddressTable.SchoolAddressKey == -1)
{myCommand.Parameters.AddWithValue("@schoolAddressKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@schoolAddressKey", mySchoolAddressTable.SchoolAddressKey);
}
if (mySchoolAddressTable.SchoolKey == null){
myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@schoolKey", mySchoolAddressTable.SchoolKey);
}
if (string.IsNullOrEmpty(mySchoolAddressTable.Address1))
{
myCommand.Parameters.AddWithValue("@address1", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@address1", mySchoolAddressTable.Address1);
}
if (string.IsNullOrEmpty(mySchoolAddressTable.Address2))
{
myCommand.Parameters.AddWithValue("@address2", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@address2", mySchoolAddressTable.Address2);
}
if (string.IsNullOrEmpty(mySchoolAddressTable.StreetName))
{
myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@streetName", mySchoolAddressTable.StreetName);
}
if (mySchoolAddressTable.TownshipCode == null){
myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@townshipCode", mySchoolAddressTable.TownshipCode);
}
if (mySchoolAddressTable.CityCode == null){
myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@cityCode", mySchoolAddressTable.CityCode);
}
if (mySchoolAddressTable.DistrictCode == null){
myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@districtCode", mySchoolAddressTable.DistrictCode);
}
if (mySchoolAddressTable.CountryCode == null){
myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@countryCode", mySchoolAddressTable.CountryCode);
}
if (string.IsNullOrEmpty(mySchoolAddressTable.PostalCode))
{
myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@postalCode", mySchoolAddressTable.PostalCode);
}
if (mySchoolAddressTable.AddressTypeCode == null){
myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@addressTypeCode", mySchoolAddressTable.AddressTypeCode);
}
if (string.IsNullOrEmpty(mySchoolAddressTable.MapAddress))
{
myCommand.Parameters.AddWithValue("@mapAddress", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@mapAddress", mySchoolAddressTable.MapAddress);
}
if (mySchoolAddressTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", mySchoolAddressTable.Status);
}
if (string.IsNullOrEmpty(mySchoolAddressTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", mySchoolAddressTable.Remark);
}
if (mySchoolAddressTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", mySchoolAddressTable.CreatedDateTime);
}
if (mySchoolAddressTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", mySchoolAddressTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a SchoolAddressTable from the database.
/// </summary>
/// <param name="schoolAddressKey">The SchoolAddressKey of the SchoolAddressTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int schoolAddressKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocSchoolAddressTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@schoolAddressKey", schoolAddressKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the SchoolAddressTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static SchoolAddressTable FillDataRecord(IDataRecord myDataRecord )
{
SchoolAddressTable mySchoolAddressTable = new SchoolAddressTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolAddressKey")))
{
mySchoolAddressTable.SchoolAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolAddressKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
{
mySchoolAddressTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Address1")))
{
mySchoolAddressTable.Address1 = myDataRecord.GetString(myDataRecord.GetOrdinal("Address1"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Address2")))
{
mySchoolAddressTable.Address2 = myDataRecord.GetString(myDataRecord.GetOrdinal("Address2"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
{
mySchoolAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
{
mySchoolAddressTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
{
mySchoolAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
{
mySchoolAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
{
mySchoolAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
{
mySchoolAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
{
mySchoolAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MapAddress")))
{
mySchoolAddressTable.MapAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("MapAddress"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
mySchoolAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
mySchoolAddressTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
mySchoolAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
mySchoolAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return mySchoolAddressTable;
}
}

 } 
